
package com.kweative.model.query;

import com.kweative.model.column.Column;
import com.kweative.util.ArrayUtil;

/**
 *
 * @author henrik
 */
public class WherePhrase<T extends Statement> extends Phrase {
    private Column col;
    private String condition;
    private Object[] values;

    public WherePhrase(Column col,Statement stmt) {
        super(stmt);
        this.col = col;
    }
    public Column getColumn() {
        return col;
    }

    public T isEqual(Object value) {
        return condition("= %s", value);
    }
    /**
     * Is equal to
     * @param value
     * @return T
     */
    public T eq(Object value) {
        return isEqual(value);
    }
    public T isNotEqual(Object value) {
        return condition("!= %s", value);
    }
    /**
     * Not equal to
     * @param value
     * @return
     */
    public T neq(Object value) {
        return isNotEqual(value);
    }

    public T isNull() {
        return condition("IS NULL");
    }
    public T isNotNull() {
        return condition("IS NOT NULL");
    }
    public T isGreaterThan(Object value) {
        return condition("> %s",value);
    }
    /**
     * Greater than
     * @param value
     * @return
     */
    public T gt(Object value) {
        return isGreaterThan(value);
    }
    public T isGreaterOrEqual(Object value) {
        return condition(">= %s",value);
    }
    /**
     * Greater than or equal to
     * @param value
     * @return
     */
    public T gtEq(Object value) {
        return isGreaterOrEqual(value);
    }
    public T isSmallerThan(Object value) {
        return condition("< %s",value);
    }
    /**
     * Smaller than
     * @param value
     * @return
     */
    public T st(Object value) {
        return isSmallerThan(value);
    }
    public T isSmallerOrEqual(Object value) {
        return condition("<= %s",value);
    }
    /**
     * Smaller than or equal to
     * @param value
     * @return
     */
    public T stEq(Object value) {
        return isSmallerOrEqual(value);
    }
    public T isBetween(Object value1,Object value2) {
        return condition("BETWEEN(%s,%s)",value1,value2);
    }
    public T between(Object value) {
        return isBetween(value, value);
    }
    public T in(Select select) {
        return condition("IN (%s)",select.compile());
    }
    public T in(Object[] values) {
        return condition("IN (%s)", ArrayUtil.join(values,"','"));
    }
    public T condition(String phrase,Object ... values) {
        this.condition = phrase;
        this.values = values;
        return (T) getStmt();
    }
    public T condition(String phrase) {
        this.condition = phrase;
        this.values = new Object[0];
        return (T) getStmt();
    }

    @Override
    public String toString() {
        return String.format(condition,QueryUtil.formatValue(col,values));
    }

}
